package app.mvc.view.components.uiControls
{
	import alternativa.gui.alternativagui;
	import alternativa.gui.base.ActiveObject;
	import alternativa.gui.base.GUIobject;
	import alternativa.gui.mouse.MouseManager;
	import alternativa.gui.mouse.dnd.IDrag;
	import alternativa.gui.mouse.dnd.IDragObject;
	
	import app.interfaces.IUIObject;
	import app.mvc.model.VO.ControlsBarVO;
	import app.mvc.model.VO.UIObjectVO;
	import app.mvc.view.components.dragDrop.DraggingObject;
	
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.events.MouseEvent;
	
	import utils.Reflection;
	
	use namespace alternativagui;
	
	public class UIObject extends ActiveObject implements IDrag,IDragObject,IUIObject
	{
		private var _model:UIObjectVO;
		private var _uiEntity:GUIobject;		//真正的GUI控件
		private var _shape:Shape;				//画选中区域的图形
		private var _fcShape:Shape;			//画可作父类容器标记
		
		//逻辑
		private var _selected:Boolean=false;
		private var _focusContainer:Boolean=false;
		
		public function UIObject($model:UIObjectVO)
		{
			super();
			init();
			if($model)
				model = $model;
		}

		private function init():void
		{
			this.mouseChildren=false;
			this.mouseEnabled=true;
		}
		
		public function setLocationXY($x:int,$y:int):void
		{
			x = $x;
			y = $y;
		}
		
		public function set model($model:UIObjectVO):void
		{
			_model = $model;
			_uiEntity = Reflection.createDisplayObjectInstance($model.classString) as GUIobject;
			
			addChild(_uiEntity);
			if(_uiEntity.hasOwnProperty("locked"))
				_uiEntity["locked"] = true;		//GUIObject 只有设置locked才能不受鼠标控制,mouseChildren,mouseEnable没用
			_uiEntity.mouseChildren = _uiEntity.mouseEnabled=false;
			_uiEntity.resize(_model.width,_model.height);
		}
		
		public function get model():UIObjectVO
		{
			return _model;
		}
		
		public function touchMe():void
		{
			trace("touchMe ",_model.isContainer);
		}
		
		override protected function draw():void
		{
			// TODO Auto Generated method stub
			super.draw();
			var gra:Graphics = graphics;
			gra.beginFill(0x000000,0);
			gra.drawRect(0,0,_model.width,_model.height);
			gra.endFill();
		}
		
		override public function drawChildren():void
		{
			// TODO Auto Generated method stub
			super.drawChildren();
		}
		
		override public function drawGraphics():void
		{
			// TODO Auto Generated method stub
			super.drawGraphics();
			resize(_model.width,_model.height);
		}
		
		/**
		 * 显示可作为父级容器
		 * @param $value
		 */
		public function set focusContainer($value:Boolean):void
		{
			if(!_fcShape)
			{
				_fcShape = new Shape();
				addChild(_fcShape);
			}
			if(_focusContainer == $value)
				return;
			_focusContainer = $value;
			var gra:Graphics = _fcShape.graphics;
			if($value)
			{
				gra.lineStyle(2,0x0000ff,0.8);
				gra.moveTo(2,2);	
				gra.lineTo(width-2,2);
				gra.lineTo(width-2,height-2);
				gra.lineTo(2,height-2);
				gra.lineTo(2,2);				
			}
			else
			{
				gra.clear();
			}			
		}
		
		public function set selected($value:Boolean):void
		{
			_selected=$value;
			
			if(!_shape)
				addChildAt(_shape = new Shape,0);
			
			var gra:Graphics = _shape.graphics;
			if(_selected)
			{
				gra.lineStyle(1,0x000000);
				gra.moveTo(-2,-2);
				gra.lineTo(width+1,-2);
				gra.lineTo(width+1,height+2);
				gra.lineTo(-2,height+2);
				gra.lineTo(-2,-2);				
			}
			else
			{
				gra.clear();
			}

		}
		
		public function getDragObject():IDragObject
		{
			// TODO Auto Generated method stub
			
			return new DraggingObject(this,this.model);
		}
		
		public function isDragable():Boolean
		{
			// TODO Auto Generated method stub
			return true;
		}
		
		public function get data():Object
		{
			// TODO Auto Generated method stub
			return null;
		}
		
		public function get graphicObject():DisplayObject
		{
			// TODO Auto Generated method stub
			return this;
		}
		
		public function get selected():Boolean
		{
			return _selected;
		}		
	}
}